<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input type="text" v-model="loginForm.username" :prefix-icon="User" placeholder="请输入用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" v-model="loginForm.password" :prefix-icon="Lock" show-password
              placeholder="请输入密码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" size="default" type="primary" class="login_btn" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue';
import { useUserStore } from '@/store/modules/user'
import { LoginDTO } from '@/api/user/type';
import { useRoute, useRouter } from 'vue-router';
import { ElNotification, FormInstance } from 'element-plus';
import { time } from '@/utils/time';

let userStore = useUserStore();
let $router = useRouter();
let $route = useRoute();
let loading = ref(false);
let loginForms = ref<FormInstance | null>(null); // 初始化为 null
let loginForm = reactive<LoginDTO>({
  username: 'admin',
  password: '123456'
});

const login = async () => {
  try {
    await loginForms.value?.validate();
    // 确保 validate 被调用时 ruleForms.value 存在
    loading.value = true;
    await userStore.userLogin(loginForm);
    let redirect: any = $route.query.redirect;
    $router.push({ path: redirect || '/' });
    ElNotification({
      message: '欢迎回来',
      title: 'Hi ' + time.getTime() + '好',
      type: 'success',
      duration: 1000
    });
  } catch (error) {
    console.error('登录失败:', error);
  } finally {
    userStore.userInfo();
    loading.value = false;
  }
};

// 表单校验规则--自定义用户名校验
const checkUsername = (_rule: any, value: any, callback: any) => {
  if (!value) {
    console.info('VALUE:', value);
    callback(new Error('请输入用户名'));
  }
  else if (value.length < 3 || value.length > 10) {
    callback(new Error('用户名长度在3到10之间'));
  } else {
    callback();
  }
}
// 表单校验规则--自定义密码校验
const checkPassword = (_rule: any, value: any, callback: any) => {
  if (!value) {
    callback(new Error('请输入密码'));

  } else
    if (value.length < 6 || value.length > 15) {
      callback(new Error('密码长度在6到15之间'));
    } else {
      callback();
    }

}
// 表单校验规则
const rules = reactive({
  username: [
    // { required: true, min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'change' }
    { trigger: 'change', validator: checkUsername }
  ],
  password: [
    // { required: true, min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'change' }
    { trigger: 'change', validator: checkPassword }
  ]
});
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
}

.login_form {
  position: relative;
  top: 30vh;
  width: 80%;
  background: url('@/assets/images/login_form.png') no-repeat;
  background-size: cover;
  padding: 40px;

  h1 {
    color: #fff;
    font-size: 40px;
  }

  h2 {
    color: #fff;
    font-size: 20px;
    margin: 20px 0px;
  }

  .login_btn {
    width: 100%;
  }
}
</style>